import request from '@/utils/request';
import URLS from './APIUrlDefine';
import { CURRENT_TOKEN } from '@/components/Authorized/renderAuthorize';

export async function GetNewsList(data: any): Promise<any> {
  return request(`${URLS.GET_NEWS_LIST}/${data.current}/${data.pageSize}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function CreateNewNews(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_NEWS, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateNews(data: any): Promise<any> {
  return request(URLS.UPDATE_NEWS, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function ChangeNewsState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_NEWS_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function DeleteNews(data: any): Promise<any> {
  return request(`${URLS.DELETE_NEWS}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function SearchGetNews(keyword: string) {
  return request(`${URLS.SEARCH_NEWS}?key=${keyword}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetTypeList(data: any): Promise<any> {
  return request(`${URLS.GET_NEWS_TYPE_LIST}/${data.current}/${data.pageSize}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function ChangeTypeState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_NEWS_TYPE_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function CreateNewType(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_NEWS_TYPE, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateType(data: any): Promise<any> {
  return request(URLS.UPDATE_NEWS_TYPE, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function DeleteType(data: any): Promise<any> {
  return request(`${URLS.DELETE_NEWS_TYPE}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function SearchGetTypeList(keyword: string) {
  return request(`${URLS.SEARCH_NEWS_TYPE}?key=${keyword}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetTagList(data: any): Promise<any> {
  return request(`${URLS.GET_TAG_LIST}/${data.current}/${data.pageSize}?type=6`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function ChangeTagState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_TAG_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function CreateNewTag(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_TAG, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateTag(data: any): Promise<any> {
  return request(URLS.UPDATE_TAG, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function DeleteTag(data: any): Promise<any> {
  return request(`${URLS.DELETE_TAG}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function SearchGetTagList(keyword: string) {
  return request(`${URLS.SEARCH_TAG}?key=${keyword}&type=6`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetNewsDetailes(id: number) {
  return request(`${URLS.GET_NEWS_DETAILES}/${id}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

